local hx__kangkai = fk.CreateSkill {
  name = "hx__kangkai",
}

hx__kangkai:addEffect(fk.TargetConfirmed, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__kangkai.name) and data.card.trueName == "slash"
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if target == player then
      if room:askToSkillInvoke(player, {
        skill_name = hx__kangkai.name,
        prompt = "#hx__kangkai-self",
      }) then
        event:setCostData(self, {tos = {player}})
        return true
      end
    else
      if room:askToSkillInvoke(player, {
        skill_name = hx__kangkai.name,
        prompt = "#hx__kangkai-invoke::"..target.id,
      }) then
        event:setCostData(self, {tos = {target}})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    target = event:getCostData(self).tos[1]
    player:drawCards(1, hx__kangkai.name)
    player.room:setPlayerMark(player, "hx__kangkai_mark", 1)
    if player:isNude() or player.dead or target.dead then return end
    local cards = room:askToCards(player, {
      skill_name = hx__kangkai.name,
      min_num = 1,
      max_num = 1,
      prompt = "#hx__kangkai-give::"..target.id,
      cancelable = false,
      include_equip = true,
    })
    if #cards > 0 then
      room:moveCardTo(cards, Card.PlayerHand, target, fk.ReasonGive, hx__kangkai.name, nil, true, player)
      if not table.contains(target:getCardIds("h"), cards[1]) or target.dead then return end
      target:showCards(cards)
      if not table.contains(target:getCardIds("h"), cards[1]) or target.dead then return end
      local card = Fk:getCardById(cards[1])
      if card.type == Card.TypeEquip and not target:isProhibited(target, card) and not target:prohibitUse(card) and
        room:askToSkillInvoke(target, {
          skill_name = hx__kangkai.name,
          prompt = "#hx__kangkai-use:::"..card:toLogString(),
        }) then
        room:useCard({
          from = target,
          tos = {target},
          card = card,
        })
      end
    end
  end,
})
hx__kangkai:addEffect(fk.CardUseFinished, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(hx__kangkai.name) and data.card.trueName == "slash" and player:getMark("hx__kangkai_mark") ~= 0
    and (target == player or target ~= player)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if data.damageDealt then
      for _, t in ipairs(data.tos) do
        if data.damageDealt[t] then
          player:drawCards(1, hx__kangkai.name)
        end
      end
    end
    room:setPlayerMark(player,"hx__kangkai_mark", 0)
  end,
})

return hx__kangkai